<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:07
 */
namespace app\api\controller;
use EasyWeChat\Factory;
use app\common\model\User as UserModel;
use app\common\model\MallConfig;
use app\common\model\ChatMoney;
use app\common\model\ChatMoneyLog;
use app\common\model\ChatMsg as ChatMsgModel;
use app\common\model\MallConfig as MallConfigModel;
use app\service\WechatService;
use app\service\PayService;


class Chat extends Base
{
  /* 禁言某用户 */
  public function forbidden () {
    if(!in_array($this->uid,explode(',',config('chat_manager')))) {
      $this->ajax(202,'无权操作');
    }
    $input = input();
    if(!$input['uid']) {
      $this->ajax(202,'参数错误');
    }
    if($input['uid']==='all') {
      MallConfig::saveConfig($this->mall_id,[
        'chat_forbidden' => 1
      ]);
      $this->ajax(200,'已开启全员禁言');
    }
    $user = UserModel::getOneById($input['uid']);
    if(!$user || $user['mall_id']!=$this->mall_id) {
      $this->ajax(202,'无权操作其他商场用户');
    }
    UserModel::where('id',$input['uid'])->update([
      'chat_forbidden' => 1
    ]);
    $this->ajax(200,'已禁言该用户');
  }

   /* 解禁某用户 */
   public function unforbidden () {
    if(!in_array($this->uid,explode(',',config('chat_manager')))) {
      $this->ajax(202,'无权操作');
    }
    $input = input();
    if(!$input['uid']) {
      $this->ajax(202,'参数错误');
    }
    if($input['uid']==='all') {
      MallConfig::saveConfig($this->mall_id,[
        'chat_forbidden' => 0
      ]);
      $this->ajax(200,'已解除全员禁言');
    }
    $user = UserModel::getOneById($input['uid']);
    if(!$user || $user['mall_id']!=$this->mall_id) {
      $this->ajax(202,'无权操作其他商场用户');
    }
    UserModel::where('id',$input['uid'])->update([
      'chat_forbidden' => 0
    ]);
    $this->ajax(200,'已解除该用户禁言');
  }

  /* 发红包 */
  public function money () {
    $input = input();
    if(!$input['money'] || !is_numeric($input['money']) || $input['money']<=0) {
      $this->ajax(202,'金额错误');
    }
    if(!$input['number'] || !is_numeric($input['number']) || $input['number']<=0) {
      $this->ajax(202,'数量错误');
    }
    if($input['money']/$input['number']<0.01) {
      $this->ajax(202,'单个红包最低0.01元');
    }
    $moneyLog = [
      'uid' => $this->uid,
      'mall_id' => $this->mall_id,
      'number' => $input['number'],
      'money' => $input['money'],
      'desc' => $input['desc'],
      'order_sn' => create_order_sn($this->uid,'money'),
      'arr' => json_encode(ChatMoney::getRandArr($input['money'],$input['number']))
    ];

    $log = ChatMoney::create($moneyLog);

    $orderConfig = [
      'body' => '发放红包',
      'out_trade_no' => $moneyLog['order_sn'],
      'total_fee' => $moneyLog['money']*100,
      'trade_type' => 'JSAPI',
      'openid' => $this->member['openid_main'],
      //'openid' => UserModel::uid2openid($moneyLog['uid']),
      'notify_url'  => request()->domain()."/notify/money/mall_id/".$this->mall_id, 
    ];
    $jsapi = PayService::S(['mall_id'=>1])->wechatJssdk($orderConfig);
    $this->ajax(200,'success',[
      'jsapi' => $jsapi
    ]);
  }

  /* 单个红包详情 */
  public function moneyDetail () {
    $input = input();
    $detail = ChatMoney::getOneById($input['id'],'uid,money,end,create_time,number,desc');
    $detail['userInfo'] = UserModel::getOneById($detail['uid'],'nickname,headimgurl,id');

    if(!$detail) {
      $this->ajax(202,'红包不存在');
    }
    $old = ChatMoneyLog::getOne([
      'uid' => $this->uid,
      'money_id' => $input['id']
    ]);
    if($old) {
      $detail['status'] = false;
      $detail['log'] = [
        'create_time' => $old['create_time'],
        'money' => $old['money']
      ];
    }else{
      $detail['status'] = true;
    }
    $detail['get_number'] =  ChatMoneyLog::getCount([
      'money_id' => $input['id']
    ]);
    $this->ajax(200,'success',$detail);
  }

  /* 抢红包 */
  public function moneyGet () {
    $input = input();
    //排队 TODO
    $hongbao = ChatMoney::getOneById($input['id']);
    $hongbao['arr'] = json_decode($hongbao['arr'],true);
    if(!$hongbao) {
      $this->ajax(202,'红包不存在');
    }
    $count = ChatMoneyLog::getCount([
      'money_id' => $input['id']
    ]);
    if(!$count) {
      $count = 0;
    }
    if($hongbao['end']) {
      $this->ajax(202,'红包已被领取完成');
    }else{
      if($count>=$hongbao['number']) {
        ChatMoney::where('id',$input['id'])->update([
          'end' => 1
        ]);
        $this->ajax(202,'红包已被领取完成');
      }
    }

    $old = ChatMoneyLog::getOne([
      'uid' => $this->uid,
      'money_id' => $input['id']
    ]);
    if($old) {
      $this->ajax(202,'您已经领取了该红包');
    }
    $log = [
      'uid' => $this->uid,
      'money_id' => $input['id'],
      'money' => $hongbao['arr'][$count]
    ];
    ChatMoneyLog::create($log);
    //type 1:现金  2:积分  3:抵扣金
    $type = config('redbag_type')==1? 1: 3;
    UserModel::changeMoney($this->uid,$type,$log['money'],$content='抢红包');
    ChatMsgModel::create([
      'uid' => $this->uid,
      'content' => json_encode([
          'type' => 'message',
          'uid' => $this->uid,
          'content' => [
            'type' => 'money_get',
            'money' => $log['money'],
            'nickname' => $this->member['nickname'],
            'uid' => $this->uid
          ]
      ]),
      'mall_id' => $hongbao['mall_id'],
      'status' => 0,
      'group_id' => UserModel::getGroupId($this->uid),
      'type' => 'money_get'
    ]);
    $count = ChatMoneyLog::getCount([
      'money_id' => $input['id']
    ]);
    if($count>=$hongbao['number']) {
      ChatMoney::where('id',$input['id'])->update([
        'end' => 1
      ]);
      $msg = ChatMsgModel::getOne([
        'other' => $input['id']
      ]);
      if($msg) {
        $content = json_decode($msg['content'],true);
        $content['content']['status'] = false;
        ChatMsgModel::where('id',$msg['id'])->update([
          'content' => json_encode($content)
        ]);
      }
      
    }
    $this->ajax(200,'领取成功',[
      'money' => $log['money'],
      'nickname' => $this->member['nickname'],
      'uid' => $this->uid
    ]);
  }

  /* 抢红包列表 */
  public function moneyLog () {
    $input = input();
    $list = ChatMoneyLog::getAll([
      'money_id' => $input['id']
    ]);
    if(!$list) {
      $this->ajax(200,'success',[]);
    }
    foreach($list as $k=>$v) {
      $list[$k]['userInfo'] = UserModel::getOneById($v['uid'],'nickname,headimgurl,id');
    }
    $this->ajax(200,'success',$list);
  }
}